package com.chujian.framework.util;

import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;

/**
 * 反射相关工具类
 *
 * @author ：chujian
 * @date ：2020-09-30
 */
public class ReflectUs {

    @SuppressWarnings("unchecked")
    public static <T> T getFieldValue(Object obj, String fieldName, Class<T> resultClass) throws Exception {
        Field field = FieldUtils.getDeclaredField(obj.getClass(), fieldName, true);
        return (T) field.get(obj);
    }

    public static void setFieldVal(Object obj, String fieldName, Object newVal) throws IllegalAccessException {
        Field field = FieldUtils.getDeclaredField(obj.getClass(), fieldName, true);
        field.set(obj, newVal);
    }

}
